package com.muyan.routing;

import com.muyan.utils.MQConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: YLTDMenglc
 * @Date: 2019/6/17
 * @Time: 14:38
 * @Description: 邮件消费者
 */
public class ConsumerEmailRouting {

    private static final String SMS_QUEUE = "eamil_queue_routing";
    // 交换机名称
    private static final String DESTINATION_NAME = "my_routing_estination";

    public static void main(String[] args) throws IOException, TimeoutException {
        System.out.println("邮件消费者启动");
        // 1. 建立mq连接
        Connection connection = MQConnectionUtils.newConnection();
        // 2.创建通道
        Channel channel = connection.createChannel();
        // 3.消费声明队列
        channel.queueDeclare(SMS_QUEUE, false, false, false, null);
        channel.queueBind(SMS_QUEUE,DESTINATION_NAME,"email");
        // 5.消费监听消息
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("短信消费者获取生产消息:" + msg);
            }
        };
        channel.basicConsume(SMS_QUEUE,true,defaultConsumer);

    }
}
